package com.cenxi.manage;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cenxi.common.convert.MenuConvert;
import com.cenxi.common.dto.MenuDTO;
import com.cenxi.common.exceptions.ServiceException;
import com.cenxi.common.query.MenuQuery;
import com.cenxi.common.result.Req;
import com.cenxi.common.result.Resp;
import com.cenxi.common.vo.MenuVO;
import com.cenxi.dao.entity.Menu;
import com.cenxi.dao.mapper.MenuMapper;
import com.cenxi.utils.TreeUtil;
import com.cenxi.utils.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author cenxi
 * @Date 2023/8/16 11:48
 * @Version 1.0
 */
@Slf4j
@Component
public class ManageMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Resource
    private MenuConvert menuConvert;

    /**
     * 查询菜单树
     * @param req
     * @return
     */
    public List<MenuVO> treeList(Req<MenuQuery> req) {
        MenuQuery data = req.getData();
        if (Objects.isNull(data)) {
            throw new ServiceException("菜单树-参数错误");
        }
        List<MenuVO> vo = menuMapper.selectList(new LambdaQueryWrapper<Menu>()
                        .eq(Objects.nonNull(data.getMenuName()), Menu::getMenuName, data.getMenuName())
                        .eq(Objects.nonNull(data.getState()), Menu::getState, data.getState())
                        .orderByDesc(Menu::getUpdateTime))
                .stream().map(item -> menuConvert.toVO(item)).collect(Collectors.toList());
        return TreeUtil.parseTree(vo);
    }

    public Resp<Boolean> create(Req<MenuDTO> req) {
        MenuDTO data = req.getData();
        if (Objects.isNull(data)) {
            return Resp.error("菜单新增-参数错误");
        }
        if (menuMapper.exists(new LambdaQueryWrapper<Menu>()
                .eq(Menu::getMenuName, data.getMenuName())
                .eq(Menu::getState, Constants.ENABLE))) {
            return Resp.error("菜单新增-菜单名称已存在");
        }
        return Resp.succeed(menuMapper.insert(menuConvert.toEntity(data)) > 0);
    }

    public Resp<MenuVO> findById(Long data) {
        return Resp.succeed(menuConvert.toVO(menuMapper.selectById(data)));
    }

    public Resp<Boolean> edit(Req<MenuDTO> req) {
        MenuDTO data = req.getData();
        if (Objects.isNull(data)) {
            return Resp.error("菜单修改-参数错误");
        }
        Menu menu = menuMapper.selectById(data.getId());
        if (Objects.isNull(menu)) {
            return Resp.error("菜单修改-id错误");
        }
        if (!menu.getMenuName().equals(data.getMenuName()) && menuMapper.exists(new LambdaQueryWrapper<Menu>()
                .eq(Menu::getMenuName, data.getMenuName())
                .eq(Menu::getState, Constants.ENABLE))) {
            return Resp.error("菜单新增-菜单名称已存在");
        }
        return Resp.succeed(menuMapper.updateById(menuConvert.toEntity(data)) > 0);
    }

    public Resp<Boolean> enable(List<Long> data, byte enable) {
        if (CollectionUtils.isEmpty(data)) {
            return Resp.error("菜单启用-id错误");
        }
        data.forEach(item -> {
            Menu menu = new Menu();
            menu.setId(item);
            menu.setState((int) enable);
            menuMapper.updateById(menu);
        });
        return Resp.succeed(Boolean.TRUE);
    }

    public Resp<Boolean> disable(List<Long> data, byte disable) {
        if (CollectionUtils.isEmpty(data)) {
            return Resp.error("菜单禁用-id错误");
        }
        data.forEach(item -> {
            Menu menu = new Menu();
            menu.setId(item);
            menu.setState((int) disable);
            menuMapper.updateById(menu);
        });
        return Resp.succeed(Boolean.TRUE);
    }

    public Resp<Boolean> deleteById(List<Long> data) {
        if (CollectionUtils.isEmpty(data)) {
            return Resp.error("菜单删除-id错误");
        }
        return Resp.succeed(menuMapper.deleteBatchIds(data) > 0);
    }
}
